package app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.KeyListener;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import app.log.APPLog;

import com.longevityassistant.activity.R;

/**
 * [自定义带友好清空按钮的Edittext。使用参见：LoginActivity中的mUserNameText]<BR>
 * [功能详细描述]
 * 
 * @author EX-HUXINWU001
 * @version [Android PABank C01, 2011-12-21]
 */
public class PAEditText extends LinearLayout implements OnClickListener,
		OnFocusChangeListener, TextWatcher {

	/**
	 * LayoutInflater对象
	 */
	private LayoutInflater mInflater;
	/**
	 * LinearLayout对象
	 */
	private LinearLayout mLinearLayout;
	/**
	 * EditText对象
	 */
	private EditText mEditText;
	/**
	 * 删除按钮
	 */
	private ImageButton mDelImgButton;
	/**
	 * 字体大小
	 */
	private int textSize;
	/**
	 * 字体颜色
	 */
	private int textColor;
	/**
	 * 提示文字
	 */
	private String hint;
	/**
	 * 提示文字颜色
	 */
	private int hintTextColor;
	/**
	 * 是否为数字
	 */
	private String numeric;
	/**
	 * 最大长度
	 */
	private int maxLength;
	/**
	 * 是否密码格式
	 */
	private boolean password;
	/**
	 * 背景
	 */
	private int background;
	/**
	 * 是否单行
	 */
	private boolean sigleline;
	/**
	 * 是否允许开始输入0
	 */
	private boolean isStartZero;
	/**
	 * 布局LinearLayout
	 */
	private LinearLayout mEditLayout;

	private int decimalLength;
	
	/**
	 * 构造方法
	 * 
	 * @param context context
	 * @param attrs  attrs
	 */
	public PAEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PAEditTextStyle);
		
		background = a.getResourceId(R.styleable.PAEditTextStyle_background, -1);
		if(background > 0){
			mEditLayout.setBackgroundResource(background);
		}
		
		String text = a.getString(R.styleable.PAEditTextStyle_text);
		if (text != null)mEditText.setText(text);
		
		textSize = a.getInt(R.styleable.PAEditTextStyle_textSize, -1);
		if(textSize > 0){
			mEditText.setTextSize(textSize);
		}else{
			mEditText.setTextSize(14);
		}
		
		hint = a.getString(R.styleable.PAEditTextStyle_hint);
		mEditText.setHint(hint);
		
		hintTextColor = a.getColor(R.styleable.PAEditTextStyle_hintTextColor, 0xFFFFFFFF);
		if(hintTextColor > 0)mEditText.setHighlightColor(hintTextColor);
		
		numeric = a.getString(R.styleable.PAEditTextStyle_numeric);
		if(numeric != null){
			if ("integer".equals(numeric)) {
				mEditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
			} else if ("decimal".equals(numeric)) {
				mEditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER|EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
			}
		}
		
		decimalLength = a.getInt(R.styleable.PAEditTextStyle_decimalLength, 2);
		
		maxLength = a.getInt(R.styleable.PAEditTextStyle_maxLength, -1);
		if (maxLength > 0) {
			mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
		}
		
		password = a.getBoolean(R.styleable.PAEditTextStyle_password, false);
		isStartZero = a.getBoolean(R.styleable.PAEditTextStyle_isStartZero, true);
		sigleline = a.getBoolean(R.styleable.PAEditTextStyle_singleLine, false);
		mEditText.setSingleLine(sigleline);
		
		mEditText.setGravity(Gravity.CENTER_VERTICAL);
		
		if (password) {
			int inputType = mEditText.getInputType();
			if(inputType > 0) {
				mEditText.setInputType(inputType|EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
			} else {
				mEditText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
			}
			mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}else{
			int inputType = a.getInt(R.styleable.PAEditTextStyle_inputType, -1);
			if(inputType > 0){
				mEditText.setInputType(inputType);
			}
		}
		
		String digits = a.getString(R.styleable.PAEditTextStyle_digits);
		if (digits != null){
			 KeyListener mInput = DigitsKeyListener.getInstance(digits.toString());
	         mEditText.setKeyListener(mInput);
		}
		
		textColor = a.getColor(R.styleable.PAEditTextStyle_textColor, Color.BLACK);
		if(textColor > 0)mEditText.setTextColor(textColor);
	}

	/**
	 * [初始化PAEditText页面控件]<BR>
	 * [功能详细描述]
	 */
	private void init() {
		mInflater = LayoutInflater.from(getContext());
		mLinearLayout = (LinearLayout) mInflater.inflate(R.layout.app_widget_pa_edittext, null);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mLinearLayout.setLayoutParams(lp);
		addView(mLinearLayout);

		mEditLayout = (LinearLayout)findViewById(R.id.edit_layout);
		mEditText = (EditText) findViewById(R.id.edit_text);
		mEditText.setOnFocusChangeListener(this);
		mEditText.addTextChangedListener(this);
		
		mDelImgButton = (ImageButton) findViewById(R.id.btn_delete);
		mDelImgButton.setOnClickListener(this);
	}

	/**
	 * [设置是否可用]<BR>
	 * [功能详细描述]
	 * @param flag
	 * @see android.view.View#setEnabled(boolean)
	 */
	public void setEnabled(boolean flag, int color){
		mEditText.setEnabled(flag);
		mEditText.setTextColor(color);
	}
	
	/**
	 * [设置是否可用]<BR>
	 * [功能详细描述]
	 * @param flag
	 * @see android.view.View#setEnabled(boolean)
	 */
	public void setEnabled(boolean flag){
		setEnabled(flag , Color.BLACK);
	}
	
	/**
	 * [得到EditText对象]<BR>
	 * [功能详细描述]
	 * @return
	 */
	public EditText getmEditText() 
	{
		return mEditText;
	}
	
	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @return
	 */
	public boolean isStartZero() {
		return isStartZero;
	}

	/**
	 * [设置首位字母去零]<BR>
	 * [功能详细描述]
	 * @param isStartZero isStartZero
	 */
	public void setStartZore(boolean isStartZero) {
		this.isStartZero = isStartZero;
	}
	
	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @return String
	 */
	public String getText() {
		return mEditText.getText().toString();
	}
	
	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * @param index
	 */
	public void setSelection(int index){
		mEditText.setSelection(index);
	}
	
	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param text
	 *            text
	 */
	public void setText(String text) {
		mEditText.setText(text);
	}
	
	/**
	 * [获得光标方法]<BR>
	 * [功能详细描述]
	 * 
	 * @param text
	 *            text
	 */
	public void setFocusable(boolean focusable) {
		mEditText.setFocusable(focusable);
	}

	/**
	 * [设置背景]<BR>
	 * [功能详细描述]
	 * 
	 * @param resid
	 *            resid
	 * @see android.view.View#setBackgroundResource(int)
	 */
	public void setBackgroundResource(int resid) {
		mEditText.setBackgroundResource(resid);
	}
	
	/**
	 * [设置背景]<BR>
	 * [功能详细描述]
	 * 
	 * @param drawable
	 *            drawable
	 * @see android.view.View#setBackgroundResource(int)
	 */
	public void setBackgroundResource(Drawable drawable) {
		mEditText.setBackgroundDrawable(drawable);
	}

	/**
	 * [设置删除按钮背景图片]<BR>
	 * [功能详细描述]
	 * 
	 * @param resid
	 *            resid
	 */
	public void setDeleteBg(int resid) {
		mDelImgButton.setBackgroundResource(resid);
	}

	/**
	 * [设置是否为密码]<BR>
	 * [功能详细描述]
	 * 
	 * @param ispassword
	 *            ispassword
	 */
	public void setPassword(boolean ispassword) {
		if (ispassword) {
			mEditText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
			mEditText.setTransformationMethod(PasswordTransformationMethod
					.getInstance());
		}
	}

	/**
	 * [设置是否为数字]<BR>
	 * [功能详细描述]
	 * @param flag flag
	 */
	public void setNumeric(boolean flag) {
		if (flag) {
			mEditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		}
	}

	/**
	 * [设置hint字符串]<BR>
	 * [功能详细描述]
	 * 
	 * @param hint
	 *            hint
	 */
	public void setHint(String hint) {
		mEditText.setHint(hint);
	}
	
	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param resid
	 *            resid
	 */
	public void setHint(int resid) {
		mEditText.setHint(resid);
	}
	
	/**
	 * [设置hint字符串颜色]<BR>
	 * [功能详细描述]
	 * @param colorid colorid
	 */
	public void setHintTextColor(int colorid)
	{
		mEditText.setHintTextColor(colorid);
	}
	
	/**
	 * [设置字符串颜色]<BR>
	 * [功能详细描述]
	 * @param colorid colorid
	 */
	public void setTextColor(int colorid)
	{
		mEditText.setTextColor(colorid);
	}
	
	/**
	 * [设置字体大小]<BR>
	 * [功能详细描述]
	 * @param size size
	 */
	public void setTextSize(float size)
	{
		mEditText.setTextSize(size);
	}

	/**
	 * [设置最大长度]<BR>
	 * [功能详细描述]
	 * 
	 * @param maxlength
	 *            maxlength
	 */
	public void setMaxLength(int maxlength) {
		if (maxlength >= 0) {
			mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxlength) });
		}
	}

	/**
	 * [设置是否单行]<BR>
	 * [功能详细描述]
	 * 
	 * @param singleLine
	 *            singleLine
	 */
	public void setSingleLine(boolean singleLine) {
		mEditText.setSingleLine(singleLine);
	}

	
	public int getDecimalLength() {
		return decimalLength;
	}

	public void setDecimalLength(int decimalLength) {
		this.decimalLength = decimalLength;
	}

	@Override
	public void onClick(View v) {
		if (v == mDelImgButton) {
			mEditText.setText("");
			mDelImgButton.setVisibility(View.GONE);
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) 
	{
		if (mEditText.getText().toString().length() > 0 && mEditText.isFocused()) {
			mDelImgButton.setVisibility(View.VISIBLE);
		} else {
			mDelImgButton.setVisibility(View.GONE);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (mEditText.getText().toString().length() > 0 && mEditText.isFocused()) 
		{
			mDelImgButton.setVisibility(View.VISIBLE);
			if(isStartZero == false)
			{
				if("0".equals(s.toString())){
					s = "";
					mEditText.setText(s);
				}
			}
			
			setDecimal(s.toString());
			
		} else {
			mDelImgButton.setVisibility(View.GONE);
		}
		
	}

	public void setDecimal(String result)
	{
		if(numeric != null && "decimal".equals(numeric))
		{
			char[] chars = result.toCharArray();
			if (!isContainsPoint(chars, ".")) {
				mEditText.setText(result.subSequence(0, result.length() - 1));
				mEditText.setSelection(mEditText.getText().toString().length());
			}
		}
	}
	
	private boolean isContainsPoint(char[] chars, String str) {
		int j = 0;
		int length = 0;
		for (int i = 0; i < chars.length; i++) {
			APPLog.i("zbkc", "char:" + chars[i]);
			if (str.equals(String.valueOf(chars[i]))) {
				j = j + 1;
				length = i + 1;
			}
		}
		// Log.i("zbkc", "jjjjjj:" + j);
		if (j > 1) {
			return false;
		} else if (j == 1 && (chars.length - length) > decimalLength) {

			return false;
		} else {
			return true;
		}

	}
	
	@Override
	public void afterTextChanged(Editable s) {
		
	}

	
}
